"use strict";

define('OrderListView', [
  'jquery', 'underscore', 'backbone', 'api',
  'OrderCollection',
  'WareListView',
  'text!templates/order/list.html'
], function ($, _, Backbone, api,
             OrderCollection,
             WareListView,
             tpl) {
  return Backbone.View.extend({

    initialize: function () {
      this.template = _.template(tpl);
    },

    events: {
      "click .pay_order": "payOrder"
    },

    payOrder: function (event) {
      console.log('payOrder');
      var target = $(event.currentTarget);
      let orderId = target.data("orderid");
      api.payOrder(orderId, function (err, data) {
        console.log(err, data);
      });
    },
    getData_orders: function (cb) {
      var orders = new OrderCollection();
      orders.fetch({
        success: function (collection) {
          cb && cb(null, collection);
        },
        error: function (coll, res) {
          cb && cb(coll, null);
        }
      });
    },

    render: function (cb) {
      var self = this;
      let data = {orders: []};

      self.getData_orders(function (err, collection) {
        if (collection) data.orders = collection.toJSON();
        console.log(data.orders);
        let tmpl = self.template(data);
        $(self.el).html(tmpl);
        cb && cb();
      });
    }
  });
});
